/**
 * 作者：
 * 创建时间：2012-4-18 上午8:34:02
 */
package dragon.core.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dragon.core.exception.ExceptionHandler;
import dragon.core.io.FileHandler;

/**
 * 实现功能：敏感词过滤
 */
public class BadWord {

	private static Logger logger = LoggerFactory.getLogger(BadWord.class);

	/**
	 * 敏感词
	 */
	private static List<String> badwords = new ArrayList<String>();

	static {
		InputStream in = FileHandler.getResourceAsStream("badwords.txt");
		String content = null;
		try {
			content = FileHandler.readFile(in, true, "UTF-8");
			String[] temp = content.split("\n");
			for (String badword : temp) {
				if (StringUtils.isEmpty(badword) || badwords.contains(badword)) {
					continue;
				}
				badwords.add(badword);
			}
			logger.info("加载了 " + badwords.size() + " 个敏感词。");
		} catch (ExceptionHandler e) {
			logger.error(e.getMessage(), e);
		}
	}

	public static void loadBadWords() {
	}

	/**
	 * 敏感词过滤
	 * 
	 * @param source
	 *            待过滤内容
	 * @return
	 * @throws ExceptionHandler
	 */
	public static String filter(String source) {
		if (StringUtils.isEmpty(source) || badwords.size() == 0)
			return source;

		for (String badword : badwords) {
			source = source.replace(badword, "*");
		}

		return source;
	}

}
